<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/10/20
 * Time: 18:41
 */
header("content-type:text/html;charset=utf-8");
require COMMON_PATH.'logTool.class.php';
require COMMON_PATH.'message.class.php';
class UserAction extends Action
{
    //用户首页
    public function index(){
        $user = isset($_SESSION['user'])?$_SESSION['user']:'';
        $rule = isset($_SESSION['rule'])?$_SESSION['rule']:'';
        if (empty($user))
            $this->error('请先登录',U('index/login'));
        if ($rule != 1)
            $this->error('您尚无权限查看此页面',U('index/index'));
        $account = M('user');
        $data = $account->where(array('is_delete' => 1))->select();
        $this->assign('list',$data);
        $this->display();
    }

    //登录验证
    public function login(){
        $back = message::fail('登录失败');
        $user = isset($_REQUEST['user'])?$_REQUEST['user'].trim():'';
        $password = isset($_REQUEST['password'])?$_REQUEST['password'].trim():'';
        $condition = array(
            'user' => $user,
            'password' => md5($password),
            'status' => 1,      //激活状态
            'delete' => 1,      //未删除状态
        );
        //验证
        $userModel = M('user');
        if ($data = $userModel->where($condition)->find()){
            $back = message::success('登录成功');
            session('user',$user);
            session('rule',$data['rule']);
            session('status',$data['status']);
        }
        logTool::logCat('登录返回信息：'.$back);
        echo $back;
        return;
    }

    //获取所有用户列表
    public function getRule(){
        $back = message::fail('获取用户列表失败');
        $user = M('user');
        $where = array(
            'delete' => 1       //未删除状态
        );
        $data = $user->where($where)->select();
        if ($data){
            if (is_null($data)){
                $back = message::success('未找到用户');
            } else {
                foreach ($data as $item=>$value){
                    if ($value['rule'] == 1){
                        $data[$item]['rule_name'] = '超级管理员';
                    }
                    if ($value['rule'] == 2){
                        $data[$item]['rule_name'] = '管理员';
                    }
                }
                $back = message::success('success',$data);
            }
        }
        logTool::logCat('查询用户列表返回信息: '.$back);
        echo $back;
        return;
    }

    //增加用户
    public function addUser(){
        $back = message::fail('增加用户失败');
        //获取参数
        $user = isset($_REQUEST['user'])?$_REQUEST['user'].trim():'';
        $password = isset($_REQUEST['password'])?$_REQUEST['password'].trim():'';
        $rule = isset($_REQUEST['rule'])?$_REQUEST['rule']:'';
        $status = isset($_REQUEST['status'])?$_REQUEST['status']:'';
        logTool::logCat('user: '.$user);
        logTool::logCat('password: '.$password);
        logTool::logCat('rule: '.$rule);
        logTool::logCat('status: '.$status);
        //任何一项为空则返回失败
        if ($user == '' || $password == '' || $rule == '' || $status == ''){
            $back = message::fail('所有选项必须填写');
            echo $back;
            return;
        }
        //查询用户名是否重复
        $account = M('user');
        $check_condition = array(
            'user' => $user
        );
        if ($account->where($check_condition)->select()){
            //用户名重复
            logTool::logCat("查重SQL：".$account->getLastSql());
            $back = message::fail('用户名已被使用');
            echo $back;
            return;
        }
        //写用户数据表
        $data = array(
            'user' => $user,
            'password' => md5($password),
            'rule' => $rule,
            'status' => $status,
            'delete' => 1
        );
        if ($account->add($data)){
            $back = message::success('新增用户成功');
        }
        logTool::logCat($account->getLastSql());
        logTool::logCat("新增用户返回信息: ".$back);
        echo $back;
        return;
    }

    //编辑用户也获取单个用户信息
    public function getSingleUser($userId){
        $back = message::fail("查询失败");
        $user = M('user');
        $data = $user->where(array('id' => $userId))->find();
        if (!$data || is_null($data)){
            $back = message::fail('查询错误，未找到该用户');
            $this->error("查询错误",U("user/index"));
        }
        $this->assign('list',$data);
        $content = $this->fetch();
        $back = message::success('查找成功',$content);
        logTool::logCat('查找单个用户返回信息: '.$back);
        echo $back;
        return;
    }

    //保存编辑用户信息
    public function saveUser(){
        $back = message::fail('保存用户失败');
        //获取参数
        $userId = isset($_REQUEST['id'])?$_REQUEST['id']:'';
        $user = isset($_REQUEST['user'])?$_REQUEST['user'].trim():'';
        $password = isset($_REQUEST['password'])?$_REQUEST['password'].trim():'';
        $rule = isset($_REQUEST['rule'])?$_REQUEST['rule']:'';
        $status = isset($_REQUEST['status'])?$_REQUEST['status']:'';
        //除密码外任何一项为空则返回失败
        if ($userId == '' || $user == '' || $rule == '' || $status == ''){
            $back = message::fail('所有选项必须填写');
            $this->error("填写错误",U("user/index"));
        }
        //写用户数据表
        if ($password == ''){
            $data = array(
                'user' => $user,
                'rule' => $rule,
                'status' => $status,
            );
        } else {
            $data = array(
                'user' => $user,
                'password' => md5($password),
                'rule' => $rule,
                'status' => $status,
            );
        }
        //更新用户表
        $userModel = M('user');
        if ($userModel->where(array('id' => $userId))->save($data)){
            $back = message::success('保存用户信息成功');
        }
        logTool::logCat("保存编辑用户返回信息: ".$back);
        echo $back;
        return;
    }

    //删除用户
    public function deleteUser($userId){
        $back = message::fail('删除用户失败');
        $user = M('user');
        if ($user->where(array('id' => $userId))->save(array('is_delete' => 0))){
            $back = message::success('删除用户成功');
        }
        logTool::logCat($user->getLastSql());
        logTool::logCat('删除用户返回信息: '.$back);
        echo $back;
        return;
    }

    public function preAddUser(){
        $content = $this->fetch();
        $back = message::success('success',$content);
        echo $back;
    }
}